//
//  WDGoutRescueViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/2.
//

#import "WDGoutRescueViewController.h"
#import "WDGoutRescueViewModel.h"
#import "WDArrivalServiceHeaderView.h"
#import "TYZBaseTableViewController.h"
#import "TYZBaseCollectionViewController.h"

@interface WDGoutRescueViewController () <TYZPageViewControllerDelegate, TYZPageViewControllerDataSource>
@property (nonatomic, strong) WDGoutRescueViewModel *viewModel;
@property (nonatomic, strong) WDArrivalServiceHeaderView *serviceTopView;
@end

@implementation WDGoutRescueViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar
{
    [super initWithVar];
    
}

- (void)initWithViewModel
{
    _viewModel = [WDGoutRescueViewModel new];
    
}

- (void)initWithNavBar
{
    [super initWithNavBar];
    
    self.navigationItem.title = self.viewModel.title;
    
    [self initWithNavRight];
}

- (void)initWithSubView
{
    [self initWithServiceTopView];
    self.dataSource = self;
    self.delegate = self;
    
    [super initWithSubView];
    
}

/// 初始化右上角的按钮
- (void)initWithNavRight
{
    if (![[EDSAppDelegateTool sharedInstance] hasScanCode]) {
        return;
    }
    UIImage *image = [UIImage imageNamed:@"ic_nav_scan_code"];
    CGRect frame = CGRectMake(0, 0, 30, 30);
    UIButton *btnRight = [UIButton createWithButton:nil frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithNavRight:) propertiesBlock:^(UIButton * _Nonnull button) {
        [button setImage:image forState:UIControlStateNormal];
    }];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
    self.navigationItem.rightBarButtonItem = rightItem;
}

/// 点击右上角按钮事件
/// - Parameter sender: sender description
- (void)actionWithNavRight:(id)sender
{
//    @weakify(self);
    void(^block)(id data) = ^(id data) {
//        @strongify(self);
        [[NSNotificationCenter defaultCenter] postNotificationName:kARRIVAL_SCAN_CODE_SUCCESS_NOTI object:data];
    };
    
    NSDictionary *params = @{
        @"fromWhere": @(2),  // 1 表示来自到站服务；2 表示来自外出救援
        @"scanTitle": @"扫码",
        @"tipInfo": @"将二维码或条形码对准扫码区域，即可自动扫码扫码配件可以确认配件的版本及适配车辆",
        @"block": block
    };
    UIViewController *vc = [[TYZRouter sharedInstance] getServiceScanCodeVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

- (void)initWithServiceTopView
{
    if (!_serviceTopView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDArrivalServiceHeaderViewHeight);
        _serviceTopView = [[WDArrivalServiceHeaderView alloc] initWithFrame:frame];
        self.headerView = _serviceTopView;
        
        @weakify(self);
        _serviceTopView.touchHeaderImageBlock = ^{
            @strongify(self);
            [self actionWithHeaderImage];
        };
    }
    [_serviceTopView updateWithViewData:self.viewModel];
}

/// 点击头像
- (void)actionWithHeaderImage
{
    [SVProgressHUD showInfoWithStatus:@"点击头像"];
}


+ (TYZPageConfigration *)setupConfig
{
    /*
    TYZPageConfigration *config = [TYZPageConfigration defaultConfigration];
    config.pageStyle = TYZPageStyleSuspensionTop;
    config.headerViewCouldScale = NO;
    config.headerViewScaleMode = TYZPageHeaderViewScaleModeTop;
    config.showTabbar = YES;
    config.showNavigation = YES;
    config.aligmentModeCenter = NO;
    config.lineWidthEqualFontWidth = YES; // 变为平分时 是否需要线条宽度等于字体宽度 默认 NO
    config.showBottomLine = NO;
    config.scrollMenu = YES;
    config.itemMargin = 20;
    
    config.aligmentModeCenter = NO;
    config.lineHeight = 2;
    config.lineWidth = 30;
    config.lineColor = [UIColor colorWithHexString:@"#19C46E"];
    
//    config.suspenOffsetY = NAVBAR_HEIGHT_STATUS; // 设置悬浮停顿偏移量
    
    config.itemFont = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    config.selectedItemFont = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    config.normalItemColor = [UIColor colorWithHexString:@"#999999"];
    config.selectedItemColor = [UIColor colorWithHexString:@"#333333"];;
    config.scrollViewBackgroundColor = kWhiteColor;
    
//    config.bottomLineHeight = 2;
//    config.bottomLineBgColor = [UIColor colorWithHexString:@"#E6E6E6"];
//    config.lineColor = [UIColor orangeColor];[UIColor colorWithHexString:@"#E6035D"];
     */
    
    TYZPageConfigration *config = [TYZPageConfigration defaultConfigration];
    config.pageStyle = TYZPageStyleSuspensionTop;
    config.headerViewCouldScale = NO;
    config.headerViewScaleMode = TYZPageHeaderViewScaleModeTop;
    config.showTabbar = YES;
    config.showNavigation = YES;
    config.aligmentModeCenter = NO;
    config.lineWidthEqualFontWidth = YES; // 变为平分时 是否需要线条宽度等于字体宽度 默认 NO
    config.showBottomLine = NO;
    config.scrollMenu = NO;
//    config.itemMargin = 20;
    
    config.aligmentModeCenter = NO;
    config.lineHeight = 2;
    config.lineWidth = 42;
    config.lineColor = kThemeColor;
    
//    config.suspenOffsetY = NAVBAR_HEIGHT_STATUS; // 设置悬浮停顿偏移量
    
    config.itemFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    config.selectedItemFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    config.normalItemColor = [UIColor colorWithHexString:@"#6C7482"];
    config.selectedItemColor = [UIColor colorWithHexString:@"#000000"];;
    config.scrollViewBackgroundColor = kWhiteColor;
    
    
    return config;
}

+ (NSArray *)setupMenuTitles {
    // 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
    
    NSMutableArray *addList = [NSMutableArray new];
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(100201) parentCode:nil]) {
        [addList addObject:@"待抢单"];
    }
    
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(100202) parentCode:nil]) {
        [addList addObject:@"待接车"];
    }
    
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(100203) parentCode:nil]) {
        [addList addObject:@"检查中"];
    }
    
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(100204) parentCode:nil]) {
        [addList addObject:@"维修中"];
    }
    
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(100205) parentCode:nil]) {
        [addList addObject:@"待报单"];
    }
    
    return [addList copy];
}

+ (NSArray *)setupViewControllers:(NSDictionary *)param {
    NSArray *titles = [self setupMenuTitles];
    NSMutableArray *vcs = @[].mutableCopy;
    for (int i=0; i<titles.count; i++) {
        NSString *title = titles[i];
        NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:param];
        mutDict[@"servFlow"] = @([self getServFlow:title]);
        mutDict[@"typeName"] = title;
        UIViewController *vc = [[TYZRouter sharedInstance] getGoutRescueItemVC:mutDict];
        [vcs addObject:vc];
    }
    return vcs;
}

+ (NSInteger)getServFlow:(NSString *)title {
    if ([title isEqualToString:@"待分派"]) {
        return 0;
    } else if ([title isEqualToString:@"待接车"]) {
        return 1;
    } else if ([title isEqualToString:@"检查中"]) {
        return 2;
    } else if ([title isEqualToString:@"维修中"]) {
        return 3;
    } else if ([title isEqualToString:@"待报单"]) {
        return 4;
    }
    return 0;
}
/// 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
//@property (nonatomic, assign) NSInteger servFlow;


#pragma mark - TYZPageViewControllerDelegate, TYZPageViewControllerDataSource
- (UIScrollView *)pageViewController:(TYZPageViewController *)pageViewController pageForIndex:(NSInteger)index
{
    UIViewController *vc = pageViewController.controllerList[index];
    
    if ([vc isKindOfClass:[TYZBaseTableViewController class]])
    {
        return ((TYZBaseTableViewController *)vc).baseTableView;
    }
    else if ([vc isKindOfClass:[TYZBaseCollectionViewController class]]) {
        return ((TYZBaseCollectionViewController *)vc).baseCollectionView;;
    }
    return nil;
}

- (void)pageViewController:(TYZPageViewController *)pageViewController contentOffsetY:(CGFloat)contentOffsetY progress:(CGFloat)progress
{
    
}

- (void)pageViewController:(TYZPageViewController *)pageViewController didScrollMenuItem:(UIButton *)itemButton index:(NSInteger)index
{
    
}

@end
